package com.app.ui.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import com.app.R
import com.app.ui.fragment.*
import com.app.util.DBOpenHelper
import okhttp3.internal.notifyAll

class HistoryActivity : AppCompatActivity() {

    val fragmentList = listOf(HomeFragment(), CovidFragment(), UserFragment())
    lateinit var dbOpenHelper: DBOpenHelper

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_history)
        val toolbar = findViewById<Toolbar>(R.id.history_tool_bar)
        setSupportActionBar(toolbar)
        // 去掉默认的标题
        title = ""
        // 设置标题的居中文字
        val realTitle = findViewById<TextView>(R.id.history_title)
        realTitle.text = "浏览历史"
        // 设置系统自带的home按钮(小箭头)可见
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        val fragment = HistoryFragment("all", "全部")
        supportFragmentManager.beginTransaction().add(R.id.historyView, fragment).commit()
    }
    // 给Activity加载标题栏的菜单项
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        //写一个menu的资源文件.然后创建就行了.
        menuInflater.inflate(R.menu.history_tool_bar_menu, menu)

        return super.onCreateOptionsMenu(menu)
    }

    //给系统自带的home按钮(小箭头)添加点击事件：销毁本页面返回上一级
    //清空历史记录
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish()
        }
        if(item.itemId == R.id.history_delete){
            var dbOpenHelper = DBOpenHelper(this)
            dbOpenHelper.deleteHistory()
        }
        return super.onOptionsItemSelected(item)
    }
}